package com.cn.mobileproject.view.dialog;

import java.util.Date;
import java.util.regex.Pattern;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.cn.mobileproject.R;
import com.cn.mobileproject.utils.DateUtils;
import com.cn.mobileproject.view.widget.DateInfo;
import com.cn.mobileproject.view.widget.NumericWheelAdapter;
import com.cn.mobileproject.view.widget.OnWheelScrollListener;
import com.cn.mobileproject.view.widget.WheelView;

public class DlgSetBirthday extends Dialog implements OnWheelScrollListener{

	private static final int m_ciDefaultYear = 1990;
	private static final int m_ciDefaultMonth = 1;
	private static final int m_ciDefaultDay = 1;

	private int m_iYear = m_ciDefaultYear;
	private int m_iMonth = m_ciDefaultMonth;
	private int m_iDay = m_ciDefaultDay;

	private Button m_btnCancel;
	private Button m_btnReset;
	private Button m_btnSet;
	private DateInfo m_curDateInfo;
	private Date m_dateBirth;
	private WheelView wheelYear;
	private WheelView wheelMonth;
	private WheelView wheelDay;
	private Context mContext;
	private OnDlgBirthDismissListener m_DismissListener;

	public interface OnDlgBirthDismissListener {
		abstract void GetDate(int iYear, int iMonth, int iDay);
	}

	public void setOnAfterDismissListener(
			OnDlgBirthDismissListener dismissListener) {
		this.m_DismissListener = dismissListener;
	}

	public DlgSetBirthday(Context context, String strDate, int Resid) {
		super(context, Resid);
		mContext = context;
		if(strDate==null||"".equals(strDate)){
			strDate = DateUtils.getCurrentTime();
		}
		getDateByString(strDate);
	}

	@SuppressWarnings("deprecation")
	private void getDateByString(String strDate) {
		if (null != strDate && 0 != strDate.length()) {
			Pattern pattern = Pattern
					.compile("//^[\\d]{4}[-\\ ][\\d]{1,2}[-\\ ][\\d]{1,2}$//");
			if (!pattern.matcher(strDate).matches()) {
				try {
					m_dateBirth = new Date(strDate.replace("-", "/"));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					m_dateBirth = null;
					e.printStackTrace();
				}
			}
		}
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.birthday_set);

		m_btnCancel = (Button) findViewById(R.id.button_cancel);
		m_btnReset = (Button) findViewById(R.id.button_reset);
		m_btnSet = (Button) findViewById(R.id.button_set);

		wheelYear = (WheelView) findViewById(R.id.wheel_year);
		wheelMonth = (WheelView) findViewById(R.id.wheel_month);
		wheelDay = (WheelView) findViewById(R.id.wheel_day);

		m_btnCancel.setOnClickListener(clickListener);
		m_btnReset.setOnClickListener(clickListener);
		m_btnSet.setOnClickListener(clickListener);

		initData();
	}

	@SuppressWarnings("deprecation")
	private void initData() {

		if(m_dateBirth != null){
			m_curDateInfo = new DateInfo(false);
			m_curDateInfo.year = m_dateBirth.getYear() + 1900;
			m_curDateInfo.month = m_dateBirth.getMonth() + 1;
			m_curDateInfo.day = m_dateBirth.getDate();
		}
		else{
			m_curDateInfo = new DateInfo(true);
		}
		
		setDate();

		initYear();
		initMonth();
		initDay();

		initDefault();
	}

	private void initDefault() {
		// TODO Auto-generated method stub
		if (wheelYear != null) {
			wheelYear.setCurrentItem(m_iYear - 1900, false);
		}

		if (wheelMonth != null) {
			wheelMonth.setCurrentItem(m_iMonth - 1, false);
		}

		if (wheelDay != null) {
			initDay();
			wheelDay.setCurrentItem(m_iDay - 1, false);
		}
	}

	private void initMonth() {
		// TODO Auto-generated method stub
		initWheel(wheelMonth, 1, 12, true);
	}

	private void initYear() {
		// TODO Auto-generated method stub
		initWheel(wheelYear, 1900, 2049, false);
	}

	private void initDay() {
		// TODO Auto-generated method stub
		int nCount = getMonthData(m_curDateInfo.year, m_curDateInfo.month);
		initWheel(wheelDay, 1, nCount, true);
		// Log.d("date", mWheelDay.getCurrentItem() + "");
		if (wheelDay.getCurrentItem() > nCount - 1) {
			wheelDay.setCurrentItem(nCount - 1, true);
		}
	}

	/**
	 * 判断某年某月有多少天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int getMonthData(int year, int month) {
		// TODO Auto-generated method stub
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			return 31;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
			return 30;
		} else {
			if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
				return 29;
			} else {
				return 28;
			}
		}
	}

	private NumericWheelAdapter initWheel(WheelView wheelView, int iStart,
			int iEnd, boolean bCyclic) {

		NumericWheelAdapter wheelAdapter = new NumericWheelAdapter(mContext,
				iStart, iEnd, "%02d");
		wheelAdapter.setTextColor(Color.BLACK);
		wheelView.setViewAdapter(wheelAdapter);
		wheelView.setCyclic(bCyclic);
		wheelView.setVisibleItems(3);
		wheelView.addScrollingListener(this);
		// wheelView.setShadowsScale(0.2f);

		return wheelAdapter;
	}

	protected Button.OnClickListener clickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			int id = v.getId();
			switch (id) {
			case R.id.button_cancel:
				dismiss();
				break;
			case R.id.button_reset:
				initDefault();
				break;
			case R.id.button_set:
				setDate();
				if (null != m_DismissListener) {
					m_DismissListener.GetDate(m_iYear, m_iMonth, m_iDay);
				}
				dismiss();
				break;
			}
		}
	};

	public void setDate() {
		m_iYear = m_curDateInfo.year;
		m_iMonth = m_curDateInfo.month;
		m_iDay = m_curDateInfo.day;
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		// TODO Auto-generated method stub
		switch (wheel.getId()) {
		case R.id.wheel_year: 
			m_curDateInfo.year = (wheel.getCurrentItem() + 1900);
			initDay();
			break;
		case R.id.wheel_month:
			m_curDateInfo.month = (wheel.getCurrentItem() + 1);
			initDay();
			break;
		case R.id.wheel_day:
			m_curDateInfo.day = (wheel.getCurrentItem() + 1);
			break;
		}
	}
}
